<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/templates/general.xhtml">
        
        <ui:define name="title"><h:outputText value="Users" /></ui:define>
        
        <ui:define name="content">
        <h:form id="form">
        	<p:growl id="growl" showDetail="false" life="3000" autoUpdate="true" />
			<p:commandButton id="newUser" rendered="#{p:ifGranted('ROLE_ADMIN')}" value="New user" onclick="newDialog.show()" style="margin: 10px;" styleClass="btn btn-success"/>
            <p:commandButton value="Back" action="back" style="margin: 10px;" styleClass="btn btn-danger" />
			<p:dataTable var="user" value="#{administrationBean.users}" style="width:400px; margin: 10px;">
				<p:column headerText="Username" style="font-weight: bold; text-align: center;">  
		            <h:outputText value="#{user.username}" />
		        </p:column> 
		        
		        <p:column headerText="Role" style="text-align: center;">  
		            <h:outputText value="#{user.role == 1 ? 'admin' : 'user'}" />
		        </p:column>
		        
		         <p:column headerText="Delete" style="text-align: center;" width="10">  
		            <p:commandLink onclick="delDialog.show()">
						<h:graphicImage value="/images/delete.png" width="25"/>
						<f:setPropertyActionListener target="#{administrationBean.user}" value="#{user}" />
					</p:commandLink>
		        </p:column>
			</p:dataTable>
			
			<p:dialog widgetVar="delDialog" modal="true" header="Delete User" resizable="false" dynamic="true">
				<p:panelGrid styleClass="noBorder">
				<p:row>
					<p:column colspan="2"><h:outputText value="Are you sure you want to delete user #{administrationBean.user.username}?" /></p:column>
				</p:row>
				<p:row>
					<p:column><p:commandButton value="Yes" actionListener="#{administrationBean.deleteUser}" onclick="delDialog.hide()" update="form" styleClass="btn btn-warning" />
					<p:commandButton value="No" onclick="delDialog.hide()" styleClass="btn btn-danger"/></p:column>
				</p:row>
				</p:panelGrid>
			</p:dialog>
			
			<p:dialog widgetVar="newDialog" modal="true" header="Create New User" resizable="false" dynamic="true">
				<p:panelGrid columns="2">
					<h:outputText value="Username"/>
					<p:inputText id="newUsername" value="#{administrationBean.user.username}" />
					<h:outputText value="Password"/>
					<p:inputText id="newPassword" type="password" value="#{administrationBean.user.password}" />
					<h:outputText value="Role"/>
					<p:selectOneMenu id="role" value="#{administrationBean.user.role}">
						<f:selectItem itemLabel="Select Role" itemValue="" />  
                		<f:selectItem itemLabel="Admin" itemValue="1" />  
                		<f:selectItem itemLabel="User" itemValue="2" />  
					</p:selectOneMenu>
				</p:panelGrid>
				<p:commandButton id="saveNewUser" value="Save" actionListener="#{administrationBean.addNewUser}" onclick="newDialog.hide()" update="form" styleClass="btn btn-success"/>
				<p:commandButton value="Calcel" onclick="newDialog.hide()" styleClass="btn btn-danger"/>
			</p:dialog>
			
			<p:commandButton value="Back" action="back" style="margin: 10px;" styleClass="btn btn-danger" />
        </h:form>
        </ui:define>
        
</ui:composition>